package cqrtplm.controller;

import com.github.pagehelper.PageInfo;

import com.ty.basic.handler.exceptionhandler.exception.ResponseCodeEnum;
import com.ty.basic.response.ResponseResult;
import cqrtplm.constant.RouteConstant;
import cqrtplm.entity.ObjectStorageDO;
import cqrtplm.service.ObjectStorageService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(RouteConstant.OBJECTSTORAGE + "v1")
public class ObjectStorageController {

    @Autowired
    private ObjectStorageService objectStorageService;

    @GetMapping("/selectAll")
    @ApiOperation(value = "查询所有存储路径", produces = "application/json; charset=utf-8")
    public ResponseResult<PageInfo> selectAll(@RequestParam(defaultValue = "1",required = false) int pageNum,
                                              @RequestParam(defaultValue = "10",required = false) int pageSize,
                                              @RequestParam(value = "typeDefName", required = false) String defName) {
        PageInfo pageInfo = objectStorageService.selectAll(pageNum, pageSize, defName);
        return ResponseResult.success(pageInfo);
    }

    @PostMapping("/insert")
    @ApiOperation(value = "插入存储路径", produces = "application/json; charset=utf-8")
    public ResponseResult<PageInfo> insert(@RequestBody ObjectStorageDO objectStorageDO) {
        try {
            objectStorageService.saveObjectStorage(objectStorageDO);
            return ResponseResult.success("插入成功");
        }catch (Exception e){
            return ResponseResult.fail("400",e.getMessage());
        }
    }
    @PostMapping("/update")
    @ApiOperation(value = "修改存储路径", produces = "application/json; charset=utf-8")
    public ResponseResult<PageInfo> update(@RequestBody ObjectStorageDO objectStorageDO) {
        try {
            objectStorageService.updateObjectStorage(objectStorageDO);
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
        return ResponseResult.success();
    }
}
